package com.halo.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

public class DocOpenServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		String pathInfo=req.getPathInfo();
		String[] params=StringUtils.split(pathInfo, "/");
		  ServletContext context=this.getServletContext();  
	        String path="D:/opt/zzword/"+params[0];
	        File file=new File(path);  
	        String nameStr=file.getName();  
	        String name=new String(nameStr.getBytes("utf-8"),"iso8859-1");  
	        FileInputStream in=new FileInputStream(file);  
	        resp.addHeader("Content-Disposition", "attachment; filename="+name);  
	        OutputStream out=resp.getOutputStream();  
	        byte[] b=new byte[8192];  
	        int temp=0;  
	        while((temp=in.read(b))!=-1){  
	            out.write(b, 0, temp);  
	        }  
	        out.flush();  
	        out.close();  
	        in.close();  
		
	}
	
}
